package top.sdaily.schedule;

/**
 * 循环队列
 * Created by admin on 2017/3/17.
 */
public class CircleQueue<T> {

    private Object[] container;
    private Integer capacity;
    private int currentPosition;

    public CircleQueue() {
        this.capacity = 3;
        this.container = new Object[capacity];
    }

    public CircleQueue(Integer capacity) {
        if (capacity == null || capacity < 3) {
            capacity = 3;
        }
        this.capacity = capacity;
        this.container = new Object[capacity];
    }

    public CircleQueue(T[] data) {
        if(data != null) {
            this.container = data;
            this.capacity = data.length;
        }
    }

    public void setData(int position, T data){
        if(position >= this.capacity) {
            throw new RuntimeException("下标超出队列长度");
        }
        this.container[position] = data;
    }

    public T getData(int position){
        if(position >= capacity) {
            throw new RuntimeException("下标超出队列长度");
        }
        return (T)container[position];
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void moveNext() {
        nextPosition();
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }

    public T getNextData() {
        return (T)container[nextPosition()];
    }

    public T getCurrentData() {
        return (T)container[currentPosition];
    }

    private Integer nextPosition() {
        currentPosition = (currentPosition + 1) % capacity ;
        return currentPosition;
    }

}
